<template>
  <div class="c-select" @click="handlerClick">
    <div class="c-select-current single-text-overflow">{{current}}</div>
    <div class="c-select-arrow">&gt;</div>
    <div class="c-select-list" ref="list" v-show="listShow && options.length !== 0">
      <div
        ref="item"
        class="c-select-item"
        v-for="(option,index) in options"
        :key="`sel-item-${index}`"
        :data-index="index"
      >{{option}}</div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'data-v-select',
  props: {
    selectedIndex: {
      type: [String, Number],
      default: 0
    },
    options: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      listShow: false,
      selIndex: this.selectedIndex
    };
  },
  computed: {
    current() {
      return this.options[this.selIndex];
    }
  },
  methods: {
    handlerClick(e) {
      // let list = this.$refs["item"];
      this.listShow = true;
      let index = e.target.dataset.index;
      if (index) {
        this.selIndex = index;
        this.listShow = false;
        this.$emit("selected", {
          selectedIndex: index,
          current: this.current
        });
      }
    }
  },
  mounted() {
    document.addEventListener("click", () => {
      this.listShow = false;
    }, true);
  }
};
</script>

<style>
/* custom select */
.c-select-group {
  display: flex;
}
.c-select {
  flex: 1;
  text-align: center;
  border: 1px solid #007FA1;
  border-radius: 3px;
  display: inline-flex;
  position: relative;
  text-align: left;
}
/* .c-select + .c-select {
  margin-left: 10px;
} */
.c-select-current {
  padding: 5px 10px;
  flex-grow: 1;
  flex-shrink: 0;
  min-height: 20px;
}
.c-select-arrow {
  width: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  transform: rotate(90deg);
}
.c-select-list {
  position: absolute;
  z-index: 1;
  width: 100%;
  max-height: 500px;
  overflow-y: auto;
  top: 36px;
  border: 1px solid #007FA1;
  background: #172434;
  border-radius: 3px;
}
.c-select-list::-webkit-scrollbar{width: 0 !important}
.c-select-item {
  font-size: 14px;
  padding: 5px 10px;
}
.c-select-item:hover {
  background: rgb(80, 113, 153);
  cursor: pointer;
}
</style>
